<template>
    <view class="content">
           推送
    </view>
    

    
    
</template>

<script>
    export default {
        data() {
            return {}
        },
        onLaunch: function() {
           
            uni.subscribePush({
                provider: "unipush",
                success: function(res) {
                    console.log("success:" + JSON.stringify(res));
                }
            });
            let osname = plus.os.name
            console.log(osname)
            if (osname == 'Android') {
                uni.onPush({
                    provider: "unipush",
                    success: function() {
                        console.log("监听透传成功");
                    },
                    callback: function(data) {
                        console.log("接收到透传数据：" + JSON.stringify(data));
                        plus.push.createMessage(data.data, {});
                    }
                });
            }
            // 监听在线消息事件
            if (osname == 'iOS') {
                plus.push.addEventListener('receive', function(msg) {
                    plus.push.createMessage(msg.content, "LocalMSG", {
                        cover: false
                    });
                    setTimeout(() => {
                        plus.push.clear();
                    }, 3000)
                }, false);
            }
            plus.push.addEventListener(
                "click",
              //这里写业务逻辑
                function(data) {
                    console.log(data);
                },
                false
            );
            plus.push.addEventListener('receive',function(res){
                    let options = {cover:false};
                    console.log(res)
                    console.log(res.content)
                    plus.push.createMessage(res.content,res.payload,options);
            },false)
        },
        // 执行事件
        methods:{
            formSubmit:function(e){
                
                var title = e.detail.value.title;
                var miaoshu = e.detail.value.miaoshu;
                var imageurl = e.detail.value.imageurl;
                var url = e.detail.value.url;
                
                uni.share({
                    provider: "weixin",
                    scene: "WXSceneSession",
                    type: 0,
                    href: url,
                    title: title,
                    summary: miaoshu,
                    imageUrl: imageurl,
                    success: function (res) {
                        console.log(JSON.stringify(res));
                        uni.showToast({
                            title: '已分享',
                            duration: 2000
                        });
                    },
                    
                    fail: function (err) {
    
                        var errrr = JSON.stringify(err);
                        if(errrr){
                            uni.showModal({
                            title: '表单不能留空',
                            content: '请完善所有信息再发起分享',
                            success: function (res) {
                                if (res.confirm) {
                                        console.log('用户点击确定');
                                    } else if (res.cancel) {
                                        console.log('用户点击取消');
                                    }
                                }
                            });
                        }
                    }
                });
            },
        },
    }
</script>

<style>
    *{margin: 0;padding: 0;}
    .content{
        width: 100%;
        margin: 20px auto;
    }
    
    .content .uni-input{
        width: 80%;
        height: 45px;
        margin: 8px auto;
        border: 1px solid #ccc;
        margin-bottom: 8px;
        padding-left: 8px;
        border-radius: 10px;
        font-size: 16px;
        color: #333;
    }
    
    .content button{
        width: 80%;
        height: 45px;
        line-height: 45px;
        margin: 8px auto;
        border: 1px solid #ccc;
        margin-bottom: 8px;
        padding-left: 8px;
        border-radius: 10px;
        font-size: 16px;
        color: #fff;
        background: #56b273;
        border: none;
    }
    
    button::after{ border: none;}
    
    .content .banquan{
        text-align: center;
        margin-top: 50px;
        font-size: 15px;
        color: #666;
    }
</style>